package com.qianda.qdminiapp.security;

import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.UUID;

/**
 * @ClassName Token
 * @Author cw
 * @Date 2019/7/1 10:08
 **/
public class Token implements Serializable {

    private static final long serialVersionUID = 1L;

    private String id;
    private long timestamp;

    /**
     * 创建默认的令牌
     */
    public Token() {
        this(
                UUID.randomUUID().toString().replaceAll("-", ""),
                System.currentTimeMillis());
    }

    /**
     * 创建令牌
     *
     * @param id        编号
     * @param timestamp 时间戳
     */
    private Token(String id, long timestamp) {
        if (StringUtils.isEmpty(id)) {
            throw new IllegalArgumentException("Id is required");
        }
        this.id = id;
        this.timestamp = timestamp;
    }

    public String getId() {
        return id;
    }

    public long getTimestamp() {
        return timestamp;
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        Token other = (Token) obj;
        return id.equals(other.id);
    }

    @Override
    public String toString() {
        return id;
    }


}
